from Commons import *
#will be used for combat
def calculateDamage(attackingcreature):#calculate the damage
    damage = 0
    for i in range(attackingcreature.damagedice):
        damage += RNG.randint(1,attackingcreature.damagedie)
    damage += attackingcreature.damagemodifier
    if (damage < 0):
        damage = 0
    return damage
def combat(attackingcreature,defendingcreature):
    attackscore = attackingcreature.attackskill
    armorboost = 0
    if (attackingcreature.inventory[EQ_WEAPON] != None):
        attackscore += attackingcreature.inventory[EQ_WEAPON].attackboost
    defensescore = defendingcreature.defenseskill
    if (defendingcreature.inventory[EQ_ARMOR] != None):
        defensescore += defendingcreature.inventory[EQ_ARMOR].defenseboost
    attack = RNG.randint(0,attackscore)
    defense = RNG.randint(0,defensescore)
    if ((attack-defense) > (0.8*attackingcreature.attackskill)):#critical, peaks out at about 50% chance for an enemy with pitiful defense
        #two attacks
        addMessage(attackingcreature.descriptor+" gets a critical hit!")
        damage = calculateDamage(attackingcreature)
        if (defendingcreature.loseHealth(damage)):#they died, stop attacking
            return attackingcreature.attackspeed
        
    if (attack > defense):
        damage = calculateDamage(attackingcreature)#damage
        defendingcreature.loseHealth(damage)
    else:
        addMessage(attackingcreature.descriptor+" misses.")
    return attackingcreature.attackspeed